﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System.Text;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class TSKD02
    {
        #region ==KUNDNA===========
        public static readonly DependencyProperty KUNDNAProperty = DependencyProperty.Register("KUNDNA",
            typeof(string), typeof(TSKD02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKUNDNAChanged));
        public string KUNDNA
        {
            get
            {
                return (string)GetValue(KUNDNAProperty);
            }
            set
            {
                SetValue(KUNDNAProperty, value);
                vwMaster.View.MoveCurrentTo(-1);
                Locate("KDKUNDNA", value);
            }
        }
        private static void OnKUNDNAChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSKD02;
            if (src != null)
            {
                src.KUNDNA = (string)e.NewValue;
            }
        }
        #endregion


        public TSKD02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKDSTAMM();
            this.DataContext = MV;
            InitializeComponent();
        }

      

        public override void LOADALL()
        {
            if (MV.TBKD == null)
            {
                SResult = ((VKDSTAMM)MV).QueryKDSTAMM(dwData, -2);
                foreach (DataRow dw in SResult.Tables[0].Rows)
                {
                    dw["KDKUNDNA"] = Helpers.Helper.GetGB2312Value(dw["KDKUNDNA"].ToString().Trim());
                }
                MergeData(SResult, false);
                //MV.TBKD = TBMain;
            }
            else
            {
                TBMain.Merge(MV.TBKD);
            }
        }

        public override void LOADBYAUF()
        { 
            Locate("KDKUNDNR", ORDERNUM);
        }

        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                LOADALL();
                Locate("KDKUNDNR", ORDERNUM);
            } 
            base.ManageCommand(sender, e);
        } 
        public void TextBlock_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //this.Focus();
            TextBox tb = sender as TextBox;
            string name = tb.Text.Trim();
            if (name.Trim().Length == 0)
                cvMaster.CustomFilter = null;
            filter = "KDKUNDNA LIKE '%" + name.ToUpper().Trim() + "%' OR KDKNAME LIKE '%" + Helpers.Helper.ConvertGB2312To8859P1(name).Trim() + "%'";
            cvMaster.CustomFilter = filter;
            vwMaster.View.MoveCurrentTo(-1); 
        } 
    }
}
